<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .box1 {
        width: 500px;
        height: 300px;
        margin: 100px auto;
        margin-bottom: 100px;
        border: 1ox solid #ccc;
    }

    li {
        height: 40px;
        cursor: pointer;
        font-size: larger;
        border: 1px solid #ccc;
        margin-bottom: 5px;
        text-align: center;
        line-height: 40px;
    }

    .box2 {
        width: 200px;
        margin: 0 auto;
        height: 100px;
        border: 1px solid #ccc;
    }

    input {
        width: 200px;
        height: 100px;
        text-align: center;
        font-size: 30px;
        color: rgb(238, 78, 15);
    }
</style>

<body>
    <div class="box1">
        <ul>
            <li>炖猪脚</li>
            <li>肥肠鸡</li>
            <li>红烧肉</li>
            <li>麻婆豆腐</li>
        </ul>
    </div>
    <div class="box2">
        <input type="text" value="0--炖猪脚">
    </div>

</body>
<script>
    var lis = document.getElementsByTagName('li');
    var input = document.getElementsByTagName('input')[0];



    // for (var i = 0; i < lis.length; i++) {
    //   lis[i].index = i;
    //   lis[i].onclick = function () {
    //     console.log(this.index);
    //   }

    // }
    for (let i = 0; i < lis.length; i++) {
        (function (i) {

            lis[i].onclick = function () {
                input.value = i + '--' + lis[i].innerHTML
                console.log(i);

            }

        })(i)

    }

</script>

</html>